package thread;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 1.7线程阻断
 * @date 2023/12/1 15:33:11
 */
public class ThreadInterrupt {

    public static void main(String[] args) throws InterruptedException {
        main2();
    }

    public static void main1() throws InterruptedException {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {

                }
            }
        });

        threadOne.start();
        threadOne.interrupt();
        System.out.println("threadOne interrupted state:" + threadOne.isInterrupted());
        threadOne.interrupt();
        System.out.println("threadOne interrupted state:" + threadOne.isInterrupted());
        System.out.println("threadOne interrupted state:" + Thread.interrupted());
        System.out.println("threadOne interrupted state:" + threadOne.isInterrupted());
        threadOne.join();
        System.out.println("main thread is over");
    }

    public static void main2() throws InterruptedException {
        Thread threadOne = new Thread(new Runnable() {
            public void run() {
                // 中断标志为 true 时会退出循环，并且清除中断标志
                while (!Thread.currentThread().isInterrupted()) {

                }
                System.out.println("threadOne isinterrupted :" + Thread.currentThread().isInterrupted());
            }
        });
        //后动线程
        threadOne.start();
        //设置中断标志
        threadOne.interrupt();
        threadOne.join();
        System.out.println("main thread is over");

    }
}
